package com.slkj.shilixiaoyuanapp.view.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.github.mikephil.charting.formatter.IFillFormatter;
import com.slkj.shilixiaoyuanapp.R;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者：何磊 on 2018/9/27 14:22
 * 描述：加载成功或者失败
 */
public class LoadSuccessAndFailDialog extends AlertDialog {
    String type;
    ImageView iv_loadimg;
    TextView tv_loadtext;
    Disposable disposable;
    protected LoadSuccessAndFailDialog(Context context) {
        super(context, R.style.normal_dialog);
    }

    protected LoadSuccessAndFailDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, R.style.normal_dialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//状态栏透明
        getWindow().setBackgroundDrawableResource(R.drawable.selector_choose_dialog_corner);
        getWindow().setLayout(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        setContentView(R.layout.dialog_loadingsuccess);
        initWidget();
        init();
    }

    private void init() {

    }

    private void initWidget() {
        iv_loadimg=findViewById(R.id.iv_loadimg);
        tv_loadtext=findViewById(R.id.tv_loadtext);
        //dialog基本设置
        setCanceledOnTouchOutside(false);
        setCancelable(false);
    }

    public static void showSuccess(Context context,String msg){
        LoadSuccessAndFailDialog dialog = new LoadSuccessAndFailDialog(context);
        dialog.show();
        dialog.tv_loadtext.setText(msg);
        Glide.with(context).load(R.drawable.loadsuccess).centerCrop().into(dialog.iv_loadimg);

    }

    public static void showFail(Context context, String msg){
        LoadSuccessAndFailDialog dialog = new LoadSuccessAndFailDialog(context);

        dialog.show();
        dialog.tv_loadtext.setText(msg);
        Glide.with(context).load(R.drawable.loadfail).centerCrop().into(dialog.iv_loadimg);
    }
    public void show(){
        super.show();

         disposable =  Observable.timer(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnComplete(() -> {
                    dismiss();
                }).subscribe();
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (disposable != null && !disposable.isDisposed()){
            disposable.dispose();
        }
    }
}
